GBML manual / Arrays

array_foreach

Runs the script action once for each place of target, from the first to the last, handing it that place's value. action is the NAME of one of your scripts and it must take exactly one argument. It is the same as writing a loop over target that calls the script with target[i].

Syntax

array_foreach(target, action)

Arguments

NameType
targetarray
actionscript

Returns

Nothing.

Description

Runs a script once for each place in target, an array, from the first place to the last. Each time, the script is handed the value in that place.

It does the same job as this loop, written once:

for (var i = 0; i < array_length(positions); i++) {
    spawn_coin(positions[i]);
}
array_foreach(positions, spawn_coin);

The script

The second thing you give is the name of one of your scripts: a file in the project's scripts folder with a function named like the file. Write just the name, with no brackets after it. Here is a script that makes a coin at a place across the screen:

// scripts/spawn_coin.gbml
function spawn_coin(x: int) {
    instance_create(x, 20, obj_coin);
}

The script has to follow three rules, and each one is checked before the game runs:

A function written out in place

You do not have to make a script file for a short job. Write the function right where the name would go:

var sum = 0;
array_foreach(nums, function (x) { sum += x; });

It is the script, only without a name. Its one parameter takes the kind of the values of the array, so you do not write the kind (you may, and then it has to fit). It can read and change the variables around it, such as a local you made on the line before, and a return inside it leaves only the function, never the event or script around it. Use a script file instead when the same job is needed in more than one place.

What happens in the awkward cases

You do thisWhat happens
give a function written out in the middle of the line, like function (x) { ... }that is fine: it is the script, only without a name (see "A function written out in place" above). It must take exactly one parameter, and it can use and change the variables around it
give the name of a variable, or of a built-in function like instance_destroyrefused when checked. It has to be one of your scripts
give a script with no parameters, or with two or morerefused when checked
give a script whose parameter is a different kind from the values, like a text parameter for an array of numbersrefused when checked, and the message says which kinds do not match
give it a list or a gridrefused when checked, and the message names the function for that kind, like list_foreach
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_foreach inside a script loops over the caller's array, whatever its size, and it can be given any of your scripts to run.

What it costs

One call of your script for each place the array has, and nothing else. No memory is set aside.

Example

var positions = array_create(3, 0);
positions[0] = 20;
positions[1] = 60;
positions[2] = 100;
array_foreach(positions, spawn_coin);   // three coins, at x 20, 60 and 100

On the GBA

A plain loop over the array_length(target) places. The script is called once for each item, from the first place to the last, as an ordinary C++ call (`game::name(item)`) inside a loop that reads the length again every time round, so a script that changed the container it was walking could not make the loop read past the end. What the script gives back is ignored. The script is the name of one of your own scripts, or a function written out in the middle of the line (function (n) { ... }), which is written out as a C++ function where it stands and can see the variables around it; either way it must take exactly one argument whose type accepts the items (a function with none written takes the items' kind). It costs one call of the script per item and nothing else: no memory is set aside.

See also: list_foreachgrid_foreacharray_filterarray_lengtharray_createArraysScriptsWriting a scriptData containers